import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { geneForRt } from '@/data/userRoutes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: () => import('@/views/LoginPage.vue')
    }
    // {
    //   path: '/context',
    //   redirect:'/home',
    //   component: () => import('@/views/ContextPage.vue'),
    //   children: [
    //     {
    //       path: '/home',
    //       name: 'home',
    //       component: HomeView
    //     },
    //     {
    //       path: '/about',
    //       name: 'about',
    //       component: () => import('@/views/AboutView.vue')
    //     },
    //     {
    //       path: '/pointRouter',
    //       component: () => import('@/views/PointRouter.vue')
    //     },
    //     {
    //       path: '/prac1',
    //       component: () => import('@/views/Prac1View.vue')
    //     },
    //     {
    //       path: '/prac11',
    //       component: () => import('@/views/Prac11View.vue')
    //     },
    //   ]
    // }
  ]
})
// 导航守卫
router.beforeEach((to, from, next) => {
  // xxxx
  console.log('检测到页面跳转', to, from)
  const children = geneForRt()
  if (!router.hasRoute('context')) {
    router.addRoute({
      path: '/context',
      name: 'context',
      redirect: '/home',
      component: () => import('../views/ContextPage.vue'),
      children: children
    })
    next(to);
    return;
  }
  // 放行
  next()
})

export default router
